package mo.leetcode_hot;

public class L416 {

    public boolean canPartition(int[] nums) {
        int n = nums.length;
        if (n < 2) {
            return false;
        }
        int sum = 0, max = 0;
        for (int num : nums) {
            sum += num;
            max = Math.max(max, num);
        }
        if (sum % 2 != 0) {
            return false;
        }
        int target = sum / 2;
        if (max > target) {
            return false;
        }
        boolean[] dp = new boolean[target+1];
        dp[0] = true;
        for (int num : nums) {
            for (int i = target; i >= num; i--) {
                dp[i] |= dp[i-num];
            }
        }
        return dp[target];
    }

    public static void main(String[] args) {
        L416 test = new L416();
        System.out.println(test.canPartition(new int[]{2, 2, 2, 4}));
    }
}
